﻿using System;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class SystemSelect : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Selection selection = activeUIDocument.Selection;
			Document document = commandData.Application.ActiveUIDocument.Document;
			if (!YJKRevitTools.ViewCheck(activeUIDocument, new List<ViewType>
			{
				(ViewType)1,
				(ViewType)2,
				(ViewType)4,
				(ViewType)3
			}))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			Transaction transaction = null;
			Result result;
			try
			{
				transaction = new Transaction(document, "系统选择");
				transaction.Start();
				RevitVersionFuncs.ClearSelection(selection);
				Reference reference = selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, new DuctAndPipeFilter(), "请点取要选择的系统管道：");
				SearchBranch searchBranch = new SearchBranch(document.GetElement(reference));
				searchBranch.DoSearch();
				ElementNode listRoot = searchBranch.GetListRoot();
				List<ElementNode> list = new List<ElementNode>();
				list = this.GetAllElemNode(listRoot, ref list);
				List<Element> list2 = new List<Element>();
				foreach (ElementNode elementNode in list)
				{
					if (!selection.GetElementIds().Contains(elementNode.Element.Id))
					{
						list2.Add(elementNode.Element);
					}
				}
				RevitVersionFuncs.SetSelection(selection, list2);
				document.Regenerate();
				transaction.Commit();
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				if (transaction != null && transaction.HasStarted() && !transaction.HasEnded())
				{
					transaction.RollBack();
				}
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			catch (Exception ex)
			{
				message = ex.Message;
				YJKMessageBox.Show(message);
				if (transaction != null && transaction.HasStarted() && !transaction.HasEnded())
				{
					transaction.RollBack();
				}
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}

		public ElementSet GetSystemElementSet(Autodesk.Revit.DB.Document doc, Reference elemRef)
		{
			string paramValue = doc.GetElement(elemRef).GetParameter(BuiltInParameter.RBS_SYSTEM_NAME_PARAM).AsString();
			return new HYSystemElementFilter(doc).GetElemSetByParamValue((BuiltInParameter)(-1140324), paramValue);
		}

		private List<ElementNode> GetAllElemNode(ElementNode eNode, ref List<ElementNode> listENode)
		{
			if (eNode == null)
			{
				return listENode;
			}
			listENode.Add(eNode);
			this.GetAllElemNode(eNode.Next, ref listENode);
			if (eNode.SubElementNode != null)
			{
				foreach (ElementNode eNode2 in eNode.SubElementNode)
				{
					this.GetAllElemNode(eNode2, ref listENode);
				}
			}
			return listENode;
		}
	}
}
